﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

using Weazel.Badger.Vhdl.Types;

namespace Weazel.Badger.Vhdl
{
  public class ArrayTypeValue : IDeclarativeItem
  {
    private Types.Type elementType;
    public Types.Type ElementType
    {
      get
      {
        return elementType;
      }

      set
      {
        elementType = value;
      }
    }

    private string name;
    private Vhdl.Types.ArrayType type;
    public Vhdl.Types.ArrayType Type
    {
      get
      {
        return type;
      }

      set
      {
        type = value;
      }
    }

    private bool isConstant;
    private List<Value> entries = new List<Value>();

    public ArrayTypeValue(string name, bool isConstant, Vhdl.Types.ArrayType type)
    {
      this.elementType = type.ElementType;
      this.name = name;
      this.isConstant = isConstant;
      this.type = type;
    }

    public void AddEntry(Value value)
    {
      if (value.Type != type.ElementType)
        throw new NotSupportedException();

      entries.Add(value);
    }

    public override string ToString()
    {
      return name;
    }

    public void Write(System.CodeDom.Compiler.IndentedTextWriter writer)
    {
      if (isConstant)
        writer.WriteLine(string.Format("constant {0} : {1} := (", name, type.ToString()));
      else
        writer.WriteLine(string.Format("{0} : {1} := (", name, type.ToString()));

      writer.Indent++;

			writer.WriteLine(string.Format("conv_std_logic_vector({0}, {1})", entries[0].ToString(), elementType.Width));

			for(int i = 1; i < entries.Count; i++)
				writer.WriteLine(string.Format(",conv_std_logic_vector({0}, {1})", entries[i].ToString(), elementType.Width));

      writer.Indent--;
      writer.WriteLine(");");
    }
  }
}
